home *** CD-ROM | disk | FTP | other *** search
/ Apple Developer Connection Student Program / ADC Tools Sampler CD Disk 3 1999.iso / Metrowerks CodeWarrior / Java Support / Java_Source / Java2 / src / java / security / GuardedObject.java < prev    next >
Encoding:
Java Source  |  1999-05-28  |  2.5 KB  |  91 lines  |  [TEXT/CWIE]

  1. /*
  2.  * @(#)GuardedObject.java    1.8 98/10/27
  3.  *
  4.  * Copyright 1997, 1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package java.security;
  16.  
  17. /**
  18.  * A GuardedObject is an object that is used to protect access to
  19.  * another object.
  20.  *
  21.  * <p>A GuardedObject encapsulates a target object and a Guard object,
  22.  * such that access to the target object is possible
  23.  * only if the Guard object allows it.
  24.  * Once an object is encapsulated by a GuardedObject,
  25.  * access to that object is controlled by the <code>getObject</code>
  26.  * method, which invokes the
  27.  * <code>checkGuard</code> method on the Guard object that is
  28.  * guarding access. If access is not allowed,
  29.  * an exception is thrown.
  30.  *
  31.  * @see Guard
  32.  * @see Permission
  33.  *
  34.  * @version 1.8 99/03/26
  35.  * @author Roland Schemers
  36.  * @author Li Gong
  37.  */
  38.  
  39. public class GuardedObject implements java.io.Serializable {
  40.  
  41.     private Object object; // the object we are guarding
  42.     private Guard guard;   // the guard
  43.  
  44.     /**
  45.      * Constructs a GuardedObject using the specified object and guard.
  46.      * If the Guard object is null, then no restrictions will
  47.      * be placed on who can access the object.
  48.      *
  49.      * @param object the object to be guarded.
  50.      *
  51.      * @param guard the Guard object that guards access to the object.
  52.      */
  53.  
  54.     public GuardedObject(Object object, Guard guard)
  55.     {
  56.         this.guard = guard;
  57.         this.object = object;
  58.     }
  59.  
  60.     /**
  61.      * Retrieves the guarded object, or throws an exception if access
  62.      * to the guarded object is denied by the guard.
  63.      *
  64.      * @return the guarded object.
  65.      *
  66.      * @exception SecurityException if access to the guarded object is
  67.      * denied.
  68.      */
  69.     public Object getObject()
  70.         throws SecurityException
  71.     {
  72.         if (guard != null)
  73.             guard.checkGuard(object);
  74.  
  75.         return object;
  76.     }
  77.  
  78.     /**
  79.      * Writes this object out to a stream (i.e., serializes it).
  80.      * We check the guard if there is one.
  81.      */
  82.     private synchronized void writeObject(java.io.ObjectOutputStream oos)
  83.         throws java.io.IOException
  84.     {
  85.         if (guard != null)
  86.             guard.checkGuard(object);
  87.     
  88.     oos.defaultWriteObject();
  89.     }
  90. }
  91.